<template>
	<view style="overflow: hidden;background-color: #F3F3F3;">
		<!-- 顶部导航 -->
		<view class="topTabBar" :style="{ position: headerPosition, top: headerTop }">
			<view class="grid" v-for="(grid, tbIndex) in orderType" :key="tbIndex" @tap="showType(tbIndex)">
				<view class="text" :style="{ color: tbIndex == tabbarIndex ? newColor.color : '', borderColor: newColor.color }" :class="[tbIndex == tabbarIndex ? 'on' : '']">
					{{ grid }}
				</view>
			</view>
		</view>
		<!-- 考虑非APP端长列表和复杂的DOM使用scroll-view会卡顿，所以漂浮顶部选项卡使用page本身的滑动 -->
		<view class="order-list">
			<view class="list">
				<view class="onorder" v-if="goodList.length == 0">
					<!-- <image src="../../../static/img/nullorder.png"></image> -->
					<image src="../../../static/img/home/notdata.png" mode=""></image>
					<view class="text">暂 无 订 单 信 息</view>
				</view>
				<view class="row" v-for="(row, index) in goodList" :key="index">
					<view class="type" v-if="row.refund == 0 && tabbarIndex !== 2 " :style="{ color: newColor.color }">{{ type[row.status + 1] }}</view>
					<view class="type" v-else-if="tabbarIndex == 2 && row.status !== 0" :style="{ color: newColor.color }">未成团</view>
					<view class="type" v-else-if="tabbarIndex == 2 && row.status == 0" :style="{ color: newColor.color }">{{ row.group_success|tuan }}</view>
					<view class="type" v-else :style="{ color: newColor.color }">{{ row.refund | refundstatus }}</view>
					<view class="order-info">
						<view class="left"><image :src="row.img" @tap="toPayment(row)"></image></view>
						<view class="right">
							<view class="name" @tap="toPayment(row)">{{ row.title }}</view>
							<view class="spec">规格:{{ row.option_title || '暂无规格'}}</view>
							<view class="price-number">
								￥
								<view class="price">{{ row.price }}</view>
								<view class="number">x{{ row.num }}</view>
							</view>
						</view>
					</view>
					<view class="detail">
						<view class="number" :style="{ color: newColor.color }">共{{ row.num }}件商品</view>
						<view class="sum" :style="{ color: newColor.color }">
							合计￥
							<view class="price" :style="{ color: newColor.color }">{{ row.total_price }}</view>
						</view>
						<view class="nominal" :style="{ color: newColor.color }"></view>
					</view>
					<view class="btns">
						<block v-if="tabbarIndex == 0">
							<!-- 需要支付的 -->
							<view class="default" v-if="row.status == 0" @tap="cancelOrder(row)">取消订单</view>
							<view class="pay" v-if="row.status == 0" @tap="toPayment(row)" :style="{ color: newColor.color, borderColor: newColor.color }">立即支付</view>
							<!-- 申请退款的 -->
							<view class="default" v-if="row.status >0 &&row.status <4 && row.refund == 0" @tap="jumprefund(row)">申请退款</view>
							<view class="pay" v-if="row.status >0 &&row.status <4 &&row.refund == 1" @tap="cencal(row)">取消退款</view>
							<!-- 申请售后的 -->
							<view class="default" v-if="row.status ==4 &&row.refund == 0" @tap="jumpre(row)">申请售后</view>
							<view class="pay" v-if="row.status ==4 &&row.refund == 1" @tap="cencal(row)">取消退款</view>
						</block>
						<block v-if="tabbarIndex == 1">
							<!-- 需要支付的 -->
							<view class="default" v-if="row.status == 0" @tap="cancelOrder(row)">取消订单</view>
							<view class="pay" v-if="row.status == 0" @tap="toPayment(row)" :style="{ color: newColor.color, borderColor: newColor.color }">立即支付</view>
						</block>
						<block v-if="tabbarIndex == 3">
							<view class="default" v-if="row.status == 1" @tap="cancelOrder(row)">取消订单</view>
						</block>
						<block v-if="tabbarIndex == 4">
							<view class="default" v-if="row.refund == 0" @tap="jumprefund(row)">申请退款</view>
							<view class="pay" v-if="row.refund == 1" @tap="cencal(row)">取消退款</view>
						</block>
						<block v-if="row.status == 4 && tabbarIndex == 5">
							<view class="default" v-if="row.refund == 0" @tap="jumpre(row)">申请售后</view>
							<view class="pay" v-if="row.refund == 1" @tap="cencal(row)">取消退款</view>
							</block>
						<block v-if="tabbarIndex == 2">
							<view class="default" @tap="cancelOrder(row)">取消订单</view>
							<!-- <view  @tap="toPayment(row)" :style="{ color: newColor.color, borderColor: newColor.color }">邀请好友</view> -->
							<button open-type="share" class="pay share" :style="{ color: newColor.color, borderColor: newColor.color }" :data-item="row">邀请好友</button>
						</block>
						<block v-if="tabbarIndex == 6">
							<view class="pay" v-if="row.refund == 1" @tap="cencal(row)">取消退款</view>
						</block>
					</view>
				</view>
			</view>
		</view>
		<ourLoading isFullScreen :active="active" text="loading..." />
	</view>
</template>
<script>
import {getUserInfo} from '@/static/utils/ahth.js'
export default {
	data() {
		return {
			headerPosition: 'fixed',
			headerTop: '0px',
			active: true,
			refund: '',
			typeText: {
				unpaid: '等待付款',
				back: '等待商家发货',
				unreceived: '商家已发货',
				received: '等待用户评价',
				completed: '交易已完成',
				refunds: '商品退货处理中',
				cancelled: '订单已取消'
			},
			type: ['已取消', '待支付', '备货中', '备货中', '配送中', '已完成'],
			orderType: ['全部', '待付款', '未成团',  '配送中', '已完成', '售后'],
			//订单列表 演示数据
			orderList: [],
			list: [],
			tabbarIndex: 0,
			status: '',
			group: '',
			goodList: [],
			pageIndex: 1,
			success:'',
			userInfo:getUserInfo()
		};
	},
	filters: {
		refundstatus(value) {
			if (value) {
				if (value == -1) {
					return '退款已拒绝';
				} else if (value == 1) {
					return '退款审核中';
				} else if (value == 2) {
					return '退款成功';
				}
			}
		},
		tuan(value){
			if(value == 0){
				return '等待成员'
			}
		}
	},
	onLoad(option) {
		//option为object类型，会序列化上个页面传递的参数
		// console.log("option: " + JSON.stringify(option));
		let tbIndex = parseInt(this.$Route.query.tbIndex) + 1 || 0;



		if(tbIndex == 3){
			this.status = 3;
		}else if(tbIndex == 0){
			this.status = ''
		}else if(tbIndex == 1){
			this.status = 0
		}else if(tbIndex == 2){
			this.status = 1
			this.group = 1
			this.success = 0
		}else if(tbIndex == 5){
			this.status =''
		}else{
			this.status = tbIndex
		}
		
		this.refund = this.$Route.query.refund || ''
		this.tabbarIndex = tbIndex;
		//兼容H5下排序栏位置
		// #ifdef H5
		let Timer = setInterval(() => {
			let uniHead = document.getElementsByTagName('uni-page-head');
			if (uniHead.length > 0) {
				this.headerTop = uniHead[0].offsetHeight + 'px';
				clearInterval(Timer); //清除定时器
			}
		}, 1);
		// #endif
	},
	onPageScroll(e) {
		return;
		//兼容iOS端下拉时顶部漂移
		this.headerPosition = e.scrollTop >= 0 ? 'fixed' : 'absolute';
	},
	onPullDownRefresh() {
		console.log('下拉刷新');
		let that = this;
		that.pageIndex = 1;
		setTimeout(function() {
			that.getOrderList();
			uni.stopPullDownRefresh();
		}, 500);
	},
	onReachBottom: function() {
		// pullScroll.success();
		console.log('上拉加载');
		let that = this;
		uni.showLoading({
			title: '加载中',
			duration: 500
		});
		setTimeout(() => {
			that.pageIndex++;
			that.request('order/getOrderList', {
				status: that.status,
				group: that.group,
				page: that.pageIndex,
				refund: that.refund,
				psize: 10,
				group_success:that.success
			}).then(res => {
				console.log(res);
				if (res.code == 200) {
					let newdata = res.data;
					that.goodList = that.goodList.concat(newdata);
				}
				uni.hideLoading();
				if (res.data.length == 0) {
					uni.showToast({
						title: '已无更多数据',
						icon: 'none'
					});
				}
			});
		}, 1000);
	},
	onShow() {
		this.pageIndex = 1
		this.getOrderList();
		setTimeout(() => {
			this.active = false;
		}, 400);
	},
	methods: {
		showType(tbIndex) {
			this.tabbarIndex = tbIndex;
			this.pageIndex = 1;
			this.status = '';
			this.group = '';
			this.refund = '';
			this.goodList = [];
			this.success = ''
			if (tbIndex == 0) {
				this.getOrderList();
			} else if (tbIndex == 1) {
				this.status = 0;
				this.getOrderList();
			} else if (tbIndex == 2) {
				this.group = 1;
				this.success = 0
				this.status = 1;
				this.getOrderList();
			}else if (tbIndex ==3) {
				this.status = 3;
				this.getOrderList();
			} else if (tbIndex == 4) {
				this.status = 4;
				this.getOrderList();
			} else if (tbIndex == 5) {
				this.refund = 1;
				this.getOrderList();
			}
		},
		jumprefund(row) {
			//申请退款
			this.$Router.push({
				path: '/pagesA/pages/order/refund',
				query: {
					orderid: row.id,
					status: 0
				}
			});
		},
		jumpre(row){//申请售后
			this.$Router.push({
				path: '/pagesA/pages/order/refund',
				query: {
					orderid: row.id,
					status: 1
				}
			});
		},
		cancelOrder(row) {
			console.log(row)
			uni.showModal({
				title: '取消订单',
				content: '确定取消此订单？',
				success: res => {
					if (res.confirm) {
						this.request('order/cancelOrder', {
							order_id: row.id
						}).then(res => {
							if (res.code == 400) {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								});
							}
							if (res.code == 200) {
								uni.showToast({
									title: '订单取消成功'
								});
								this.pageIndex = 1
								this.getOrderList();
							}
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		doCancelOrder(ordersn) {
			let typeNum = this.orderList.length;
			for (let i = 0; i < typeNum; i++) {
				let list = this.orderList[i];
				let orderNum = list.length;
				if (orderNum > 0 && list[0].type == 'unpaid') {
					for (let j = 0; j < orderNum; j++) {
						if (this.orderList[i][j].ordersn == ordersn) {
							this.orderList[i][j].type = 'cancelled';
							break;
						}
					}
				}
			}
		},
		toPayment(row) {
			//本地模拟订单提交UI效果
			uni.showLoading({
				title: '正在获取订单...'
			});
			var paymentOrder = [];
			console.log(row);
			setTimeout(() => {
				uni.hideLoading();
				this.$Router.push({
					path: '/pagesA/pages/order/orderDetails',
					query: {
						orderid: row.id
					}
				});
			}, 500);
		},
		getOrderList() {
			//获取订单列表
			console.log('执行了');
			this.request('order/getOrderList', {
				status: this.status,
				group: this.group,
				page: this.pageIndex,
				refund: this.refund,
				psize: 10,
				group_success:this.success
			}).then(res => {
				console.log(res);
				this.goodList = res.data;
			});
		},
		cencal(row) {
			//取消退款
			uni.showModal({
				title: '取消订单',
				content: '确定取消退款申请？',
				success: res => {
					if (res.confirm) {
						this.request('order/cancelRefund', {
							order_id: row.id
						}).then(res => {
							console.log(res);
							if(res.code == 200){
								uni.showToast({
									title:'取消退款成功!'
								})
								this.getOrderList()
							}
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	},
	onShareAppMessage: function(ret) {
		//**** 在这里配置弹出的分享框的内容
		console.log(ret);
		let uid = this.userInfo.user_id || '';
		let goodId = ret.target.dataset.item.goods_id
		let img =  ret.target.dataset.item.img[0]
		let teamId = ret.target.dataset.item.team_id
		return {
			title: '社区保障供应', // 设置弹出框的标题
			path: `/pagesA/pages/goods/onjoin?pid=${uid}&goodid=${goodId}&teamId=${teamId}`, //设置弹出框分享出去的链接地址
			imageUrl: img, //设置弹出框分享出去的图片
			success: function(res) {
				console.log(res);
			}
		};
	},
};
</script>

<style lang="scss">
page {
	background-color: #f3f3f3;
}
.topTabBar {
	width: 100%;
	position: fixed;
	top: 0;
	z-index: 10;
	background-color: #ffffff;
	height: 80upx;
	display: flex;
	justify-content: space-around;
	.grid {
		width: 20%;
		height: 80upx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #999999;
		font-size: 28upx;
		.text {
			height: 76upx;
			display: flex;
			align-items: center;
			&.on {
				color: #f23a3a;
				border-bottom: solid 4upx #f23a3a;
			}
		}
	}
}
.order-list {
	margin-top: 80upx;
	padding-top: 20upx;
	width: 100%;
	.list {
		width: 100%;
		margin: 0 auto;
		.onorder {
			width: 100%;
			height: 50vw;
			display: flex;
			justify-content: center;
			align-content: center;
			flex-wrap: wrap;
			margin-top: 100upx;
			image {
				width: 220upx;
				height: 190upx;
			}
			.text {
				width: 100%;
				height: 60upx;
				font-size: 28upx;
				font-family: Droid Sans Fallback;
				font-weight: 400;
				color: rgba(153, 153, 153, 1);
				line-height: 60upx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
		.row {
			width: calc(100% - 40upx);
			padding: 10upx 20upx;
			border-radius: 10upx;
			background-color: #fff;
			margin: 0 auto;
			margin-bottom: 20upx;
			box-sizing: content-box;
			overflow: hidden;
			.type {
				height: 50upx;
				// display: flex;
				align-items: center;
				text-align: right;
				font-size: 28upx;
				font-family: Droid Sans Fallback;
				font-weight: bold;
				color: rgba(242, 58, 58, 1);
				line-height: 50upx;
			}
			.order-info {
				width: 100%;
				display: flex;
				.left {
					flex-shrink: 0;
					// width: 25vw;
					// height: 25vw;
					width: 200upx;
					height: 200upx;
					image {
						// width: 25vw;
						// height: 25vw;
						width: 200upx;
						height: 200upx;
						border-radius: 10upx;
					}
				}
				.right {
					width: 100%;
					margin-left: 20upx;
					position: relative;
					.name {
						width: 100%;
						font-size: 28upx;
						min-height: 65upx;
						display: -webkit-box;
						-webkit-box-orient: vertical;
						-webkit-line-clamp: 2;
						overflow: hidden;
					}
					.spec {
						color: #a7a7a7;
						font-size: 24upx;
					}
					.price-number {
						position: absolute;
						bottom: 0;
						width: 100%;
						display: flex;
						color: rgba(51, 51, 51, 1);
						align-items: flex-end;
						font-size: 28upx;
						.price {
							margin-right: 5upx;
							font-size: 28upx;
							font-family: Droid Sans Fallback;
							font-weight: 400;
							color: rgba(51, 51, 51, 1);
						}
						.number {
							font-size: 22upx;
							font-family: Droid Sans Fallback;
							font-weight: 400;
							color: rgba(153, 153, 153, 1);
						}
					}
				}
			}
			.detail {
				display: flex;
				justify-content: flex-end;
				align-items: flex-end;
				height: 60upx;
				font-size: 24upx;
				color: #f23a3a;
				.sum {
					padding: 0 8upx;
					display: flex;
					align-items: flex-end;
					.price {
						font-size: 30upx;
					}
				}
			}
			.btns {
				overflow: hidden;
				display: flex;
				align-items: center;
				justify-content: flex-end;
				padding: 10upx 0;
				view {
					min-width: 120upx;
					height: 50upx;
					padding: 0 20upx;
					border-radius: 50upx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28upx;
					margin-left: 20upx;
				}
				.default {
					border: solid 1upx #ccc;
					color: #666;
				}
				.pay {
					border: solid 1upx #f23a3a;
					color: #f23a3a;
				}
				.share{
					margin: 0;
					min-width: 120upx;
					height: 50upx;
					padding: 0 20upx;
					border-radius: 50upx;
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28upx;
					margin-left: 20upx;
					background-color: #FFFFFF;
					&:active{
						background-color: #FFFFFF;
					}
				}
			}
		}
	}
}
</style>
